package com.examples.program;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.examples.tools.ConfigurationManager;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

public class Driver {

	private static SQLServerDataSource dataSource = new SQLServerDataSource();

	static {
		dataSource.setServerName(ConfigurationManager.getValue("server"));
		dataSource.setDatabaseName(ConfigurationManager.getValue("database"));
		dataSource.setUser(ConfigurationManager.getValue("username"));
		dataSource.setPassword(ConfigurationManager.getValue("password"));
	}

	public static void main(String[] args) {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = dataSource.getConnection();
			final String sqlCmd = "SELECT * FROM Employees";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sqlCmd);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				int age = resultSet.getInt("age");
				String first = resultSet.getString("first");
				String last = resultSet.getString("last");
				System.out.println("ID : " + id);
				System.out.println("Age : " + age);
				System.out.println("FirstName : " + first);
				System.out.println("LastName : " + last);
				System.out.println("-------------------------------");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null) resultSet.close();
				if (statement != null) statement.close();
				if (connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}
}
